using System;
using System.Collections.Generic;
using System.Text;
using TidhiGanitam.DataModel;
using System.Collections;

namespace TidhiGanitam.Ganitam
{
    public class Chandrudu
    {
        Rasi m_MadyamaChandra;
        Rasi m_ChandraKendraGati;
        Rasi m_SayanaCSudhaRahuvu;
        ArrayList m_DailyRoutines;
        DateTime m_Ugadi;

        public Rasi MadyamaChandrudu
        {
            get
            { return m_MadyamaChandra; }
            set
            { m_MadyamaChandra = value; }
        }
        public Rasi ChandraKendraGati
        {
            get
            { return m_ChandraKendraGati; }
            set
            { m_ChandraKendraGati = value; }
        }
        public Rasi SayanaCShudhaRahuvu
        {
            get
            { return m_SayanaCSudhaRahuvu; }
            set
            { m_SayanaCSudhaRahuvu = value; }
        }
        public ArrayList DailyCalculations
        {
            get
            { return m_DailyRoutines; }
            set
            { m_DailyRoutines = value; }
        }
        public Chandrudu()
        {
            m_MadyamaChandra = new Rasi();
            m_ChandraKendraGati = new Rasi();
            m_SayanaCSudhaRahuvu = new Rasi();
            m_DailyRoutines = new ArrayList();
        }
        public Chandrudu(Rasi MadyamaChandrudu, Rasi ChandraKendraGati, Rasi SayanaCSudhaRahuvu)
        {
            m_MadyamaChandra = MadyamaChandrudu;
            m_ChandraKendraGati = ChandraKendraGati;
            m_SayanaCSudhaRahuvu = SayanaCSudhaRahuvu;
            m_DailyRoutines = new ArrayList();
        }
        public void DoChandraSamskaram(int Year, Abdapudu Abdapudu, Ravi Ravi, YearDetails CurrentYearDetails)
        {
            /********************************************************
             * 1. Get MadyamaChandrudu, ChandraKendraGati, SayanaCSudhaRahuvu for the Salivahana year
             * 2. These are Sayana Grahalu - With Iyanamsa
             * 3. Chalana Phala Samskaram
             * 4. Tidhula Samskaram
             * 5. Iyamamsa Samskaram
             * 6. Chandra 7 kostaka samskaram
             * 7. Spasta Chandra Calculation
             * *******************************************************/
            //Set the ugadi date
            m_Ugadi = Abdapudu.UgadiDate;
            //Step 1. Salivahana sakam value calculation
            ChandraItem chandraItem = SaliVahanaSakam.GetChandraValues(Year);
            m_MadyamaChandra = chandraItem.MadhyamaChandrudu;
            m_ChandraKendraGati = chandraItem.ChandraKendraGati;
            m_SayanaCSudhaRahuvu = chandraItem.SayanaCSudhaRahuvu;

            //step 3. Chalanaphalam Calculation
            Rasi TempChalanaPhalam = new Rasi(0, Abdapudu.ChalanaPhalam.RawValue);
            Rasi MadyamaChandraChalanam = TempChalanaPhalam * Constants.ChandraDinaGati;
            Rasi ChandraKendraGatiChalanam = TempChalanaPhalam * Constants.ChandraKendraDinaGati;

            MadyamaChandraChalanam = new Rasi(MadyamaChandraChalanam.Rasulu, MadyamaChandraChalanam.Bhagalu,
                            MadyamaChandraChalanam.Liptalu,MadyamaChandraChalanam.Viliptalu);
            ChandraKendraGatiChalanam = new Rasi(ChandraKendraGatiChalanam.Rasulu, ChandraKendraGatiChalanam.Bhagalu,
                ChandraKendraGatiChalanam.Liptalu,ChandraKendraGatiChalanam.Viliptalu);

            if (Abdapudu.ChalanaPhalamSign == Enums.ChalanaPhalam.Runam)
            {
                m_MadyamaChandra -= MadyamaChandraChalanam;
                m_ChandraKendraGati -= ChandraKendraGatiChalanam;
            }
            else
            {
                m_MadyamaChandra += MadyamaChandraChalanam;
                m_ChandraKendraGati += ChandraKendraGatiChalanam;
            }

            //Step 4 . Tidhi samskaram
            Rasi MadyamaChandraTidhiGati = Constants.ChandraDinaGati * Abdapudu.Tidhulu;
            Rasi ChandraKendraTidhiGati = Constants.ChandraKendraDinaGati * Abdapudu.Tidhulu;

            m_MadyamaChandra -= MadyamaChandraTidhiGati;
            m_ChandraKendraGati -= ChandraKendraTidhiGati;

            
            //Step 5. Iyanamsa samskaram
            //For the timebeing im calculating the iyanamsa for 2009
            m_MadyamaChandra -= CurrentYearDetails.UgadiIyanamsamu;

            //Step 6. Chandra 7 Kostakam Samskaram
            //if ravi kendram > 5 subtract values otherwise add values
            Rasi Chandra7KostakamValue = Constants.GetChandra7KostakamValue(Ravi.RaviKendram);
            Chandra7KostakamValue = new Rasi(Chandra7KostakamValue.Round());
            if (Ravi.RaviKendram.Rasulu > 5)
            {
                m_MadyamaChandra -= Chandra7KostakamValue;
                m_ChandraKendraGati -= Chandra7KostakamValue;
            }
            else
            {
                m_MadyamaChandra += Chandra7KostakamValue;
                m_ChandraKendraGati += Chandra7KostakamValue;
            }
            m_MadyamaChandra = new Rasi(m_MadyamaChandra.Rasulu, m_MadyamaChandra.Bhagalu,
                m_MadyamaChandra.Liptalu,m_MadyamaChandra.Viliptalu);
            m_ChandraKendraGati = new Rasi(m_ChandraKendraGati.Rasulu, m_ChandraKendraGati.Bhagalu,
                m_ChandraKendraGati.Liptalu,m_ChandraKendraGati.Viliptalu);
        }
        public void DoChandraRoutinesForYear(Ravi Ravi)
        {
            int iSNo = 0;
            ArrayList ArrChandra = new ArrayList();
            Rasi SpastaChandrudu = new Rasi(m_MadyamaChandra);
            Rasi ChandraKendram = new Rasi(m_ChandraKendraGati);
            for (; iSNo < 400; iSNo++)
            {
                ChandraRoutineItem ChandraRoutine = new ChandraRoutineItem(iSNo, SpastaChandrudu, ChandraKendram,
                    (RaviRoutineItem)Ravi.DailyCalculations[iSNo], m_Ugadi.AddDays(iSNo));
                ChandraRoutine.Calculate();
                ArrChandra.Add(ChandraRoutine);
                SpastaChandrudu += Constants.ChandraDinaGati;
                ChandraKendram  += Constants.ChandraKendraDinaGati;
                SpastaChandrudu.Refresh();
                ChandraKendram.Refresh();
            }
            m_DailyRoutines = ArrChandra;
        }
    }
}
